<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="84px"
        :rules="formRules"
      >
        <el-form-item label="套餐编号" prop="mealSn">
          <el-input
            disabled
            v-model="formData.mealSn"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="套餐名称" prop="mealName">
          <el-input
            disabled
            v-model="formData.mealName"
            placeholder="请输入套餐名称"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="图片" prop="mealPic">
          <material-picker disabled v-model="formData.mealPic" />
        </el-form-item>
        <el-form-item label="配菜描述" prop="mealDescribe">
          <el-input
            disabled
            v-model="formData.mealDescribe"
            placeholder="请输入配菜描述"
            clearable
            :maxlength="30"
          />
        </el-form-item>
        <el-form-item label="套餐原价" prop="price">
          <el-input-number
            controls-position="right"
            :min="0"
            :step="1"
            :precision="2"
            v-model="formData.price"
            placeholder="请输入套餐原价"
            clearable
          />
        </el-form-item>
        <el-form-item label="优惠价格" prop="preferentialAmount">
          <el-input-number
            controls-position="right"
            :min="0"
            :step="1"
            :precision="2"
            v-model="formData.preferentialAmount"
            placeholder="请输入优惠价格"
            clearable
          />
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input-number
            controls-position="right"
            :min="0"
            :step="1"
            :precision="0"
            v-model="formData.stock"
            placeholder="请输入库存"
            clearable
          />
        </el-form-item>
        <el-form-item label="排序" prop="seq">
          <el-input-number
            controls-position="right"
            :min="0"
            :max="999"
            :step="1"
            :precision="0"
            v-model="formData.seq"
            placeholder="请输入排序"
            clearable
          />
          <span style="color: #f58f8f; font-size: 12px" class="ml-4"
            >数值越<span style="font-size: 14px; color: red; font-weight: bold"
              >小</span
            >显示越靠前，取值范围：0~999</span
          >
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import feedback from "@/utils/feedback";

const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑" : "新增";
});
const formData = reactive({
  id: "",
  mealId: -1 as number,
  isDelete: 0 as number,
  mealSn: "" as string | number,
  mealName: "",
  mealPic: "",
  mealDescribe: "",
  price: 0 as number,
  preferentialAmount: 0 as number,
  actualAmount: 0 as number,
  salesNum: 0 as number,
  stock: 0 as number,
  seq: 0 as number,
});
const formRules = {
  mealName: [
    {
      required: true,
      message: "请输入套餐名称",
      trigger: ["blur"],
    },
  ],
  mealPic: [
    {
      required: true,
      message: "请上传图片",
      trigger: ["change"],
    },
  ],
  mealDescribe: [
    {
      required: true,
      message: "请输入配菜描述",
      trigger: ["blur"],
    },
  ],
  price: [
    {
      required: true,
      message: "请输入套餐原价",
      trigger: ["blur"],
    },
  ],
  preferentialAmount: [
    {
      required: true,
      message: "请输入优惠价格",
      trigger: ["blur"],
    },
  ],
  /* actualAmount: [
    {
      required: true,
      message: "请输入实际价格",
      trigger: ["blur"],
    },
  ],
  salesNum: [
    {
      required: true,
      message: "请输入本次销售数据",
      trigger: ["blur"],
    },
  ],
  stock: [
    {
      required: true,
      message: "请输入库存",
      trigger: ["blur"],
    },
  ], */
};

const handleSubmit = async () => {
  await formRef.value?.validate();
  if (formData.price - formData.preferentialAmount < 0) {
    feedback.msgWarning("优惠价格不能大于原价");
  } else {
    handleClose();
    feedback.msgSuccess("编辑完成");
    emit("success", formData);
  }
};

const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
});
</script>
